package com.cwb.study.springbootstudy.utill;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * JSONObject序列化包含Date类型数据的Java对象
 * https://blog.csdn.net/w592376568/article/details/81178593
 * 使用方法
 * JsonConfig jsonConfig = new JsonConfig();
 * jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
 * JSONObject json = new JSONObject();
 * json.fromObject(object, jsonConfig)
 */
public class JsonDateValueProcessor implements JsonValueProcessor {
    private String format = "yyyy-MM-dd HH:mm:ss";

    public JsonDateValueProcessor() {
        super();
    }

    public JsonDateValueProcessor(String format) { // 自己需要的格式
        super();
        this.format = format;
    }

    @Override
    public Object processArrayValue(Object value, JsonConfig paramJsonConfig) {
        return process(value);
    }

    @Override
    public Object processObjectValue(String key, Object value, JsonConfig paramJsonConfig) {
        return process(value);
    }

    private Object process(Object value) {
        if (value instanceof Date) {
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
            return sdf.format(value);
        }
        return value == null ? "" : value.toString();
    }

}
